package com.magpiebridge.controller;

import com.magpiebridge.pojo.User;
import com.magpiebridge.pojo.Usermsg;
import com.magpiebridge.service.UsermsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class UsermsgController {
    @Autowired
    private UsermsgService usermsgService;

    @GetMapping("/getAllUsermsg")
    public List<Usermsg> getAllUsermsg(){
        return  usermsgService.getAllUsermsg();
    }

    @GetMapping("/getUsermsgById/{id}")
    public Usermsg getUserById(@PathVariable Integer id){
        return usermsgService.getUsermsgById(id);
    }

    @PostMapping("/insertUsermsg")
    public String insertUsermsg(@RequestBody Usermsg usermsg){

         Integer id = usermsg.getId();
         String name = usermsg.getName();
         String sex = usermsg.getSex();
         Integer age = usermsg.getAge();
         String edubg = usermsg.getEdubg();
         Integer high = usermsg.getHigh();
         Integer weigh = usermsg.getWeigh();
         String address = usermsg.getAddress();
         String work = usermsg.getWork();
         Integer inmoney = usermsg.getInmoney();
         String marry = usermsg.getMarry();
         String qiwang = usermsg.getQiwang();
         String chefang = usermsg.getChefang();
         String introduce= usermsg.getIntroduce();
         String xingzuo = usermsg.getXingzuo();

        int result = usermsgService.insertUsermsg(usermsg);
        if (result > 0){
            return "发布成功";
        } else {
            return "发布失败";
        }
    }

    @DeleteMapping("/deleteUsermsg/{id}")
    public  String deleteUsermsg(@PathVariable  Integer  id)  {
        try  {
            //  查询发布信息是否存在
            Usermsg  usermsg  =  usermsgService.getUsermsgById(id);
            if  (usermsg  ==  null)  {
                return  "发布信息不存在";
            }
            //  删除发布信息
            int  result  =  usermsgService.deleteUsermsg(id);
            if  (result  >  0)  {
                //  更新数据库状态
                return  "删除成功";
            }  else  {
                return  "删除失败";
            }
        }  catch  (Exception  e)  {
            //  记录日志或警告
            //  TODO:  Log  deletion  information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

    @PostMapping("/updateUsermsg/{id}")
    public String updateUsermsg(@PathVariable Integer id, @RequestBody Usermsg newUsermsg) {
        try {
            // 查询发布信息是否存在
            Usermsg usermsg = usermsgService.getUsermsgById(id);
            if (usermsg == null) {
                return "发布信息不存在";
            }
            // 更新发布信息
            usermsg.setId(newUsermsg.getId());
            usermsg.setName(newUsermsg.getName());
            usermsg.setSex(newUsermsg.getSex());
            usermsg.setAge(newUsermsg.getAge());
            usermsg.setEdubg(newUsermsg.getEdubg());
            usermsg.setHigh(newUsermsg.getHigh());
            usermsg.setWeigh(newUsermsg.getWeigh());
            usermsg.setAddress(newUsermsg.getAddress());
            usermsg.setWork(newUsermsg.getWork());
            usermsg.setInmoney(newUsermsg.getInmoney());
            usermsg.setMarry(newUsermsg.getMarry());
            usermsg.setQiwang(newUsermsg.getQiwang());
            usermsg.setChefang(newUsermsg.getChefang());
            usermsg.setIntroduce(newUsermsg.getIntroduce());
            usermsg.setXingzuo(newUsermsg.getXingzuo());

            int result = usermsgService.updateUsermsg(usermsg);
            if (result > 0) {
                return "更新成功";
            } else {
                return "更新失败";
            }
        } catch (Exception e) {
            // 记录日志或警告
            // TODO: Log update information
            // 打印错误信息
            System.err.println(e.getMessage());
            return "系统错误";
        }
    }

}
